package fr.insee.rome.string.norm;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Set;

import fr.insee.rome.factory.RomeFactory;

public class TxtFileNormalizer implements FileNormalizer {

	private RomeNormalizer normalizer;
	
	
	
	public TxtFileNormalizer() {
		normalizer = RomeFactory.getNormalizer();
	}


	@Override
	public void normaliser(String input, String output) throws IOException {
		
		InputStream stream = new FileInputStream(input);
		Reader reader = new InputStreamReader(stream);
		BufferedReader buffer = new BufferedReader(reader);
		FileWriter writer = new FileWriter(output);
		
		String line = null;
		while((line = buffer.readLine()) != null){
			
			String[] tokens = line.split("=");
			Set<String> mots = normalizer.normaliser(tokens[1]);
			String chaine = "";
			for (String mot : mots) {
				chaine = chaine + mot + " ";
			}
			chaine =chaine.trim() + "\n";
			writer.append(chaine);
			tokens = null;
		}
		
		writer.close();
		buffer.close();
		reader.close();
		stream.close();
		
		writer = null;
		buffer = null;
		reader = null;
		stream = null;
	}

}
